﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using jSLCore.Client.Presentation.jBinding;

namespace jSLCore.Client.Presentation.BaseControl.DialogMessage
{
    /// <summary>
    /// Standart dialog box for entering text value
    /// </summary>
    public partial class jAskForStringBox : DialogControl
    {
        private readonly jValidationRule _rule;
        private readonly IValueConverter _converter;

        public jAskForStringBox(object headerText, string oldValue,bool isMultiLine):this(headerText,oldValue, isMultiLine, null,null)
        {
            
        }

        public jAskForStringBox(object headerText, string oldValue,bool isMultiLine,jValidationRule rule,IValueConverter converter)
        {
            _rule = rule;
            _converter = converter;
            InitializeComponent();
            header.Header = headerText ?? string.Empty;
            if (oldValue != null)
            {
                txt.Text = oldValue;
                if (converter != null) txt.Text = converter.Convert(oldValue,typeof(string),null,CultureInfo.CurrentUICulture).ToString();
            }
            txt.AcceptsReturn = isMultiLine;
            if (isMultiLine)
            {

                txt.MinHeight = 70;
                header.KeyDown += (sender, args) => { if(args.Key==Key.Enter)args.Handled = true; };//because someone think it will insert \n, but ut will handled by ok button
                txt.TextWrapping = TextWrapping.Wrap;
                txt.VerticalScrollBarVisibility=ScrollBarVisibility.Auto;
            }
        }
        
        /// <summary>
        /// Only for designer
        /// </summary>
        public jAskForStringBox()
        {
            InitializeComponent();
        }

        public override void LoadInitialData()
        {
            if(_converter!=null)
            {
                var binding = (jNullContextBinding)GetjBinding(txt);
                binding.Converter = _converter;
            }

            if(_rule!=null)
            {
                var binding = (jNullContextBinding)GetjBinding(txt);
                binding.ValidationRules.Add(_rule);
               
            }

             Bind();
        }

        public override bool IsOkVisible
        {
            get { return true; }
        }

        public override void OkClicked()
        {
            object result = txt.Text;

            if(_converter!=null)
            {
                try
                {
                    result = _converter.ConvertBack(txt.Text, typeof(object), null, CultureInfo.CurrentUICulture);
                }
                catch (NotImplementedException)
                {
                    
                }
                
            }

            InvokeDataSelected(result);
        }

        public override bool IsStandartOkCancelEnabled
        {
            get { return true; }
        }

        public override bool AutoSize
        {
            get { return true; }
        }

        public override Control GetControlForInitialFocus()
        {
            return txt;
        }
    }
}
